package com.aiyi.education.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.aiyi.education.user.model.SysLoginModel;
import com.alibaba.fastjson.JSONObject;
import com.education.util.MD5Util;
import com.education.RandImageUtil;
import com.education.util.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author renyong
 * @email sxgyry@163.com
 * @date  2020-04-22
 */
@RestController
@RequestMapping("/sys")
@Api(tags="用户登录")
@Slf4j
public class LoginController {
    @Autowired
    private RedisUtil redisUtil;

    private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";

    @ApiOperation("登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<JSONObject> login(@RequestBody SysLoginModel sysLoginModel){
        Result<JSONObject> result = new Result<JSONObject>();
        String username = sysLoginModel.getUsername();
        String password = sysLoginModel.getPassword();
              String captcha = sysLoginModel.getCaptcha();
        redisUtil.set("hah", "renyong", 60);
        redisUtil.set("redis","renyongjie");
        Object value=redisUtil.get("username");
        if(captcha==null){
            result.error500("验证码无效");
            return result;
        }
        String lowerCaseCaptcha = captcha.toLowerCase();
        return result;
    }


    /**
     * 短信登录接口
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/smslogin",method = RequestMethod.POST)
    public Result<String> sms(@RequestBody JSONObject jsonObject) {
        Result<String> result = new Result<String>();
        String mobile = jsonObject.get("mobile").toString();
        String smsmode=jsonObject.get("smsmode").toString();
        log.info(mobile);
        if(StringUtils.isEmpty(mobile)){
            result.setMessage("手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }

        return result;
    }

    /**
     * 获取校验码
     */
    @ApiOperation("获取验证码")
    @GetMapping(value = "/getCheckCode")
    public Result<Map<String,String>> getCheckCode(){
        Result<Map<String,String>> result = new Result<Map<String,String>>();
        Map<String,String> map = new HashMap<String,String>();
        try {
            String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
            String key = MD5Util.MD5Encode(code+System.currentTimeMillis(), "utf-8");
            redisUtil.set(key, code, 60);
            map.put("key", key);
            //update-begin-author：taoyan date:20200210 for:TASK #3391 【bug】安全问题，返回验证码不安全
            String encode = java.util.Base64.getEncoder().encodeToString(code.getBytes("UTF-8"));
            map.put("code",encode);
            //update-end-author：taoyan date:20200210 for:TASK #3391 【bug】安全问题，返回验证码不安全
            result.setResult(map);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
        return result;
    }

    /**
     * 后台生成图形验证码
     * @param response
     * @param key
     */
    @ApiOperation("获取验证码2")
    @GetMapping(value = "/randomImage/{key}")
    public Result<String> randomImage(HttpServletResponse response, @PathVariable String key){
        Result<String> res = new Result<String>();
        try {
            String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
            String lowerCaseCode = code.toLowerCase();
            String realKey = MD5Util.MD5Encode(lowerCaseCode+key, "utf-8");
            redisUtil.set(realKey, lowerCaseCode, 60);
            String base64 = RandImageUtil.generate(code);
            res.setSuccess(true);
            res.setResult(base64);
        } catch (Exception e) {
            res.error500("获取验证码出错"+e.getMessage());
            e.printStackTrace();
        }
        return res;
    }
}
